package edu.imac.angryfood.world;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import edu.imac.angryfood.component.AbstractProjectile;
import edu.imac.angryfood.component.AcceleratedProjectile;
import edu.imac.angryfood.component.BombProjectile;
import edu.imac.angryfood.component.BoomerangProjectile;
import edu.imac.angryfood.component.DividedProjectile;
import edu.imac.angryfood.component.SimpleProjectile;
import edu.imac.angryfood.render.Camera;

/** A class to handle the Keyboard inputs */

public class KeyboardInputs {
	
	private Input input;
	/** a boolean to see when the mouse is pressed if the click just occured */
	private Boolean mousePressed;
	/** X position of the click when the mouse is pressed */
	private int mouseXBegin;
	/** Y position of the click when the mouse is pressed */
	private int mouseYBegin;
	/** X position of the click when the mouse is released */
	private int mouseXEnd;
	/** Y position of the click when the mouse is released */
	private int mouseYEnd;
	/** current X position of mouse */
	private int mouseX1;
	/** X position of mouse at the previous step */
	private int mouseX2;
	/** current Y position of mouse */
	private int mouseY1;
	/** X position of mouse at the previous step */
	private int mouseY2;

	/**
	 * 
	 * @param input the Input
	 */
	public KeyboardInputs(Input input) {
		this.input=input;
		mousePressed=false;
	}

	public Input getInput() {
		return input;
	}


	/** the action that occurs when the mouse is pressed	
	 * @param world the world that contains the game
	 * @param container the container of the game
	 * @param camera the camera
	 * @throws SlickException
	 */
	public void mouseDown(World world, GameContainer container, Camera camera) throws SlickException{
		for (AbstractProjectile proj : world.getProjectiles()){
			//we take the current projectile
			if (proj.getNum()==AbstractProjectile.current){
				// conversion to put the slick axis at then center
		    	int mouseX = (int) ((input.getMouseX()-container.getWidth()/2)-proj.getJboxBody().getWorldCenter().x*camera.getScale()+camera.getX());
		    	int mouseY = (int) ((-(input.getMouseY()-container.getHeight()/2)-proj.getJboxBody().getWorldCenter().y*camera.getScale())+camera.getY());
				//if the click is on the projectile
				if (Math.sqrt(Math.pow(mouseX, 2)+Math.pow(mouseY, 2))<proj.getRadius()*camera.getScale() || Math.sqrt(Math.pow(mouseX, 2)+Math.pow(mouseY, 2))<proj.getWidth()*camera.getScale() || Math.sqrt(Math.pow(mouseX, 2)+Math.pow(mouseY, 2))<proj.getHeight()*camera.getScale()){
					//if the click juste occured
					if (!mousePressed){	
						mousePressed=true;
						//if the projectile has not been already droped						
						if (proj.isDragable()){	
							proj.setDraged(true);							
							//the coordinates of the click are savec
							mouseXBegin=input.getMouseX();
							mouseYBegin=input.getMouseY();					
							mouseX1=input.getMouseX();
							mouseY1=input.getMouseY();
							
						}
					}
				}
						
				
				//if the projectile is being draged
				if (proj.getDraged()){
					
					//the mouse coordinates are saved
					mouseX2=mouseX1;
					mouseY2=mouseY1;					
					
					//the projectile cannot go further the launching area
					if (mouseXBegin-input.getMouseX()>=0 && mouseXBegin-input.getMouseX()<=(60*camera.getScale())){						
						mouseX1=input.getMouseX();					
					}
					else if (mouseXBegin-input.getMouseX()<0){
						mouseX1=mouseXBegin;
					}
					else if (mouseXBegin-input.getMouseX()>(60*camera.getScale())){
						mouseX1=(int) (mouseXBegin-(60*camera.getScale()));
					}
					
					if (mouseYBegin-input.getMouseY()>=(-50*camera.getScale()) && mouseYBegin-input.getMouseY()<=(25*camera.getScale())){
						mouseY1=input.getMouseY();
					}
					else if (mouseYBegin-input.getMouseY()<(-50*camera.getScale())){
						mouseY1=(int) (mouseYBegin+(50*camera.getScale()));
					}
					else if (mouseYBegin-input.getMouseY()>25*camera.getScale()){
						mouseY1=(int) (mouseYBegin-(25*camera.getScale()));
					}
					
					//translation of the projectile according to the mouse coordinates, without going further the launching area
					proj.translate((mouseX1-mouseX2)/camera.getScale(),(mouseY2-mouseY1)/camera.getScale());
				}
			}
		}
	}
	
	/**
	 * The action that occurs when the mouse is not pressed
	 * @param world the world that contains the game
	 * @param camera the camera
	 * @throws SlickException
	 */
	public void mouseUp(World world, Camera camera) throws SlickException{
		//if the mouse has just been released
		if (mousePressed){
			mousePressed=false;
			for (AbstractProjectile proj : world.getProjectiles()){
				//we take the current projectile
				if (proj.getNum()==AbstractProjectile.current){		
					//if it is being draged
					if (proj.getDraged()){
						camera.setProjFollowed(AbstractProjectile.current);
						//the projectile is being droped
						proj.setDroped(true);
						proj.setDraged(false);
						mouseXEnd=mouseX2;
						mouseYEnd=mouseY2;
						world.remove(proj);
						//a new projectile that is not static is created, and launched
						if (proj instanceof SimpleProjectile){
							if(proj.getShapeType().equals("Rectangle"))
								proj=new SimpleProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getWidth(), proj.getHeight(),true, proj.getNum(), proj.getImgName());
							else
								proj=new SimpleProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getRadius(),true, proj.getNum(), proj.getImgName());
							
						}
						if (proj instanceof BombProjectile)
							if(proj.getShapeType().equals("Rectangle"))
								proj=new BombProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getWidth(), proj.getHeight(),true, proj.getNum(), proj.getImgName());
							else
								proj=new BombProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getRadius(),true, proj.getNum(), proj.getImgName());
						if (proj instanceof DividedProjectile){
							DividedProjectile divided = (DividedProjectile)proj;
							if(proj.getShapeType().equals("Rectangle"))
								proj=new DividedProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getWidth(), proj.getHeight(),true, proj.getNum(), proj.getImgName(), divided.getWidthMini(), divided.getHeightMini(), divided.getImagesMini());
							else
								proj=new DividedProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getRadius() ,true, proj.getNum(), proj.getImgName(), divided.getImagesMini());
						}
						if (proj instanceof BoomerangProjectile)
							if(proj.getShapeType().equals("Rectangle"))
								proj=new BoomerangProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getWidth(), proj.getHeight(),true, proj.getNum(), proj.getImgName());
							else
								proj=new BoomerangProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getRadius(),true, proj.getNum(), proj.getImgName());
						if (proj instanceof AcceleratedProjectile)
							if(proj.getShapeType().equals("Rectangle"))
								proj=new AcceleratedProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getWidth(), proj.getHeight(),true, proj.getNum(), proj.getImgName());
							else
								proj=new AcceleratedProjectile((int) (AbstractProjectile.xInitXML+(mouseXEnd-mouseXBegin)/camera.getScale()), (int) (AbstractProjectile.yInitXML+(mouseYBegin-mouseYEnd)/camera.getScale()),proj.getRadius(),true, proj.getNum(), proj.getImgName());
						world.add(proj);
						if(proj.getShapeType().equals("Circle"))
							proj.launch((float)(-1.2*proj.getJboxBody().m_mass*(mouseXEnd-mouseXBegin)/camera.getScale()),(float)(1.2*proj.getJboxBody().m_mass*(mouseYEnd-mouseYBegin)/camera.getScale()), world);
						if(proj.getShapeType().equals("Rectangle"))
							proj.launch((float)(-1.2*proj.getJboxBody().m_mass*(mouseXEnd-mouseXBegin)/camera.getScale()),(float)(1.2*proj.getJboxBody().m_mass*(mouseYEnd-mouseYBegin)/camera.getScale()), world);
						
						if (proj instanceof SimpleProjectile){
							proj.click(world);
						}
						
					}	
					
				}
			}
	
		}
		
	}
}
